#! /bin/sh
#
# susan-ctl-sd This shell script takes care of starting and stopping
#		the susan Storage daemon
#
#   This is pretty much watered down version of the RedHat script
#   that works on Solaris as well as Linux, but it won't work everywhere.
#
# description: Backup Archiving REcovery Open Sourced.
#

PSCMD="@PSCMD@"
PS="ps"

#
# On Solaris, you may need to use nawk, or alternatively,
#  add the GNU binaries to your path, such as /usr/xpg4/bin
#
AWK=@AWK@

# All these are not *really* needed but it makes it
#  easier to "steal" this code for the development
#  environment where they are different.
#
SUSAN_STORAGEDAEMON_BINARY=${SUSAN_STORAGEDAEMON_BINARY:-@sbindir@/susan-sd}
PIDDIR=${PIDDIR:-@piddir@}
SUBSYSDIR=@subsysdir@

SUSAN_CONFIG_DIR=${SUSAN_CONFIG_DIR:-@confdir@}
SUSAN_SD_PORT=${SUSAN_SD_PORT:-@sd_port@}
SUSAN_SD_USER=${SUSAN_SD_USER:-@sd_user@}
SUSAN_SD_GROUP=${SUSAN_SD_GROUP:-@sd_group@}

Susan="@SUSAN@"
PIDOF="@PIDOF@"
PGREP="@PGREP@"

OS=`uname -s`

#
# Source the generic functions.
#
. @scriptdir@/susan-ctl-funcs

# if /lib/tls exists, force Susan to use the glibc pthreads instead
if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
   export LD_ASSUME_KERNEL=2.4.19
fi

case "$1" in
    start)
        if [ -x ${SUSAN_STORAGEDAEMON_BINARY} ]; then
            echo "Starting the $Susan Storage daemon"
            OPTIONS=''
            if [ "${SUSAN_SD_USER}" != '' ]; then
                OPTIONS="${OPTIONS} -u ${SUSAN_SD_USER}"
            fi

            if [ "${SUSAN_SD_GROUP}" != '' ]; then
                OPTIONS="${OPTIONS} -g ${SUSAN_SD_GROUP}"
            fi

            if [ "${SUSAN_CONFIG_DIR}" != '' ]; then
                OPTIONS="${OPTIONS} -c ${SUSAN_CONFIG_DIR}"
            fi

            if [ "x${VALGRIND_SD}" = "x1" ]; then
                valgrind --leak-check=full ${SUSAN_STORAGEDAEMON_BINARY} -v $2 $3 ${OPTIONS}
            else
                ${SUSAN_STORAGEDAEMON_BINARY} -v $2 $3 ${OPTIONS}
            fi
        fi
        ;;

    stop)
        if [ -x ${SUSAN_STORAGEDAEMON_BINARY} ]; then
            echo "Stopping the $Susan Storage daemon"
            killproc ${SUSAN_STORAGEDAEMON_BINARY} ${SUSAN_SD_PORT}
        fi
        ;;

    restart)
        $0 stop
        sleep 5
        $0 start
        ;;

    status)
        [ -x ${SUSAN_STORAGEDAEMON_BINARY} ] && status ${SUSAN_STORAGEDAEMON_BINARY} ${SUSAN_SD_PORT}
        exit $?
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

exit 0

